package com.jsgt.greekfire.ui.mine.user.activation

import android.app.Activity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.event.TransferEvent
import com.jsgt.greekfire.factory.UserViewModelFactory
import com.jsgt.greekfire.ui.mine.user.activation.adapter.ActivationAdapter
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.recyclerview.recyclerview
import org.greenrobot.eventbus.EventBus

/**
 * 激活列表
 * author fhx
 * create time 2020/4/26 19:01
 **/
class ActivationListActivity : BaseActivity() {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(
            this,
            UserViewModelFactory()
        ).get(UserViewModel::class.java)
    }

    override fun getLayoutId(): Int = R.layout.recyclerview

    override fun main() {
        showTitleView()
        title = getString(R.string.activation_list)
        recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        val activationAdapter =
            ActivationAdapter()
        activationAdapter.viewClickListener = {
            userViewModel.activityTransfer2(it.guid!!)
        }

        recyclerview.adapter = activationAdapter
        userViewModel.activityTransferList()
        userViewModel.activityTransferList.observe(this, Observer {
            activationAdapter.addData(it)
        })
        userViewModel.activityTransfer2LiveData.observe(this, Observer {
            ToastUtils.show(this, "激活成功")
            EventBus.getDefault().post(TransferEvent(""))
            setResult(Activity.RESULT_OK)
            finish()
        })
    }
}